Avoid per-connect lock on shared Bootstrap options map (#2218)#2219
Merged
hyperxpro merged 4 commits intoJul 18, 2026
Merged
Conversation
…nt#2218) AHC reuses a single Bootstrap for every outbound connection. Netty's AbstractBootstrap.newOptionsArray() copies the shared options map under "synchronized (options)" on every connect, serializing all connection attempts on one monitor. Under high connection-establishment rates this becomes a lock convoy. Resolve the configured ChannelOptions once at construction into a fixed array and apply them to each channel from the existing channel initializer via Channel.config().setOption(...), leaving the Bootstrap options map empty. This removes the global lock from the connect path and avoids re-reading config per connection, while preserving identical option values, ordering, and timing (options are applied during channel registration, before connect).
…nt#2218) AHC reuses a single Bootstrap for every outbound connection. Netty's AbstractBootstrap.newOptionsArray() copies the shared options map under "synchronized (options)" on every connect, serializing all connection attempts on one monitor. Under high connection-establishment rates this becomes a lock convoy. Resolve the configured ChannelOptions once at construction into a fixed array and apply them to each channel from the existing channel initializer via Channel.config().setOption(...), leaving the Bootstrap options map empty. This removes the global lock from the connect path and avoids re-reading config per connection, while preserving identical option values, ordering, and timing (options are applied during channel registration, before connect).
Contributor
Author
|
Hi @hyperxpro could you check this PR. Thanks in advance |
Member
|
I will have a look soon along with your other PRs. |
This was referenced Jul 18, 2026
hyperxpro
added a commit
that referenced
this pull request
Jul 18, 2026
Motivation After #2219, channel options are applied per-channel in ChannelManager.applyChannelOptions via Channel.config().setOption(...) instead of Bootstrap#option. The direct call dropped the error handling that Netty's AbstractBootstrap#setChannelOption provided: an unknown option key is now silently ignored (previously logged Unknown channel option), and an option whose setOption throws now propagates out of initChannel as a generic channel-init failure that closes the channel with a masked cause, instead of being logged with the offending option and value. Modification In applyChannelOptions, mirror Netty's AbstractBootstrap#setChannelOption: warn and skip when setOption returns false (unknown option), and warn (naming the option and value) then rethrow on failure, preserving Netty's close-on-failure semantics so a channel never connects with a partially-applied configuration. Result Channel-option error handling and diagnostics match the pre-#2219 Bootstrap behavior again. Happy-path behavior is unchanged, and only user-supplied config.getChannelOptions() can reach these branches since the built-in defaults are guarded to valid ranges.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2218.
AsyncHttpClient reuses a single
Bootstrapinstance for every outboundconnection. On each connect, Netty's
AbstractBootstrap.newOptionsArray()copies the shared options map under
synchronized (options):Because the same Bootstrap (and its options map) backs every connection,
this is a single global monitor that all connection attempts contend on.
Under high connection-establishment rates — many concurrent outbound
requests, short-lived connections with low reuse, or no effective per-host
connection cap — threads queue on this lock and a convoy forms.
Fix
Stop configuring options on the Bootstrap. Instead:
fixed array (buildChannelOptions). Conditional options (connect timeout,
SO_LINGER, send/receive buffers) and all config getters are evaluated a
single time rather than per connection.
initializer via the public Channel.config().setOption(...) API
(applyChannelOptions), leaving the Bootstrap options map empty.
This removes the global lock from the connect path entirely while preserving
identical behavior:
applied last so they override defaults, matching the previous Bootstrap
order.
registration (handlerAdded, channel already registered), which is before
the connect listener fires, so options are set before the channel connects.
The connect-timeout option is honored because Netty reads it at connect time.
channel initializer into its pipeline.
Scope is limited to ChannelManager.
Benefits
(fewer per-connect allocations).